package com.wanpinghui.supplier.user;

import android.content.DialogInterface;
import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.common.BaseActivity;
import com.wanpinghui.supplier.utils.WTimeUtil;
import com.wanpinghui.supplier.widgets.TitlePanelLayoutUser;
import com.wanpinghui.wphlibrary.WPHSdk;
import com.wanpinghui.wphlibrary.common.dialog.BaseDialogManager;
import com.wanpinghui.wphlibrary.common.dialog.YunBaseDialog;
import com.wanpinghui.wphlibrary.common.toast.ToastManager;
import com.wanpinghui.wphlibrary.model.base.pay.PayManager;
import com.wanpinghui.wphlibrary.model.oauth.OAuthService;
import com.wanpinghui.wphlibrary.model.oauth.bean.UserBean;
import com.wanpinghui.wphlibrary.model.user.UserInfoService;
import com.wanpinghui.wphlibrary.model.user.bean.GetRechargeResult;
import com.wanpinghui.wphlibrary.model.user.bean.RechargePackBean;
import com.wanpinghui.wphlibrary.model.user.bean.RechargePlansBean;
import com.yunshl.yunshllibrary.utils.NumberUtil;
import com.yunshl.yunshllibrary.utils.TextUtil;
import com.yunshl.yunshllibrary.utils.TimeUtil;

import org.xutils.common.util.DensityUtil;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import retrofit.callback.YRequestCallback;

/***
 * 功能描述:
 * 作者:qiujialiu
 * 时间:2018/1/2
 ***/
@ContentView(R.layout.layout_activity_minepackage_new)
public class MinePackageActivity2 extends BaseActivity implements PayManager.PayResultListener {
    @ViewInject(R.id.rl_charge_promotion)
    private RelativeLayout mLayoutPromotion;
    @ViewInject(R.id.tpl_title)
    private TitlePanelLayoutUser mLayoutTitle;
    @ViewInject(R.id.tv_bid_point)
    private TextView textViewBidPoint;
    @ViewInject(R.id.tv_sms_point)
    private TextView textViewSmsPoint;
    @ViewInject(R.id.tv_dispatch_point)
    private TextView textViewDispatchPoint;
    @ViewInject(R.id.tv_left_dispatch_point)
    private TextView textViewLeftDispatchPoint;
    @ViewInject(R.id.ll_dispatch_pack_one)
    private LinearLayout linearLayoutDispatchPackOne;
    @ViewInject(R.id.ll_dispatch_pack_two)
    private LinearLayout linearLayoutDispatchPackTwo;
    @ViewInject(R.id.tv_dispatch_explain)
    private TextView textViewDispatchExplain;
    @ViewInject(R.id.iv_copper)
    private ImageView imageViewCopper;
    @ViewInject(R.id.ll_recharge_one)
    private LinearLayout linearLayoutRechargeOne;
    @ViewInject(R.id.ll_recharge_two)
    private LinearLayout linearLayoutRechargeTwo;
    @ViewInject(R.id.ll_recharge_three)
    private LinearLayout linearLayoutRechargeThree;
    @ViewInject(R.id.tv_bid_point_pack_one)
    private TextView textViewBidPointPackOne;
    @ViewInject(R.id.tv_bid_point_pack_two)
    private TextView textViewBidPointPackTwo;
    @ViewInject(R.id.tv_bid_point_pack_three)
    private TextView textViewBidPointPackThree;
    @ViewInject(R.id.tv_bid_point_price_one)
    private TextView textViewBidPointPriceOne;
    @ViewInject(R.id.tv_bid_point_price_two)
    private TextView textViewBidPointPriceTwo;
    @ViewInject(R.id.tv_bid_point_price_three)
    private TextView textViewBidPointPriceThree;
    @ViewInject(R.id.ll_sms_pack_one)
    private LinearLayout linearLayoutSmsPackOne;
    @ViewInject(R.id.ll_sms_pack_two)
    private LinearLayout linearLayoutSmsPackTwo;
    @ViewInject(R.id.tv_sms_point_one)
    private TextView textViewSmsPointOne;
    @ViewInject(R.id.tv_sms_point_two)
    private TextView textViewSmsPointTwo;
    @ViewInject(R.id.tv_sms_point_price_one)
    private TextView textViewSmsPriceOne;
    @ViewInject(R.id.tv_sms_point_price_two)
    private TextView textViewSmsPriceTwo;
    @ViewInject(R.id.ll_sms_pack_three)
    private LinearLayout linearLayoutSmsPackThree;
    @ViewInject(R.id.tv_sms_point_three)
    private TextView textViewSmsPointThree;
    @ViewInject(R.id.tv_sms_point_price_three)
    private TextView textViewSmsPriceThree;
    @ViewInject(R.id.tv_dispatch_point_one)
    private TextView textViewDispatchPointOne;
    @ViewInject(R.id.tv_dispatch_point_two)
    private TextView textViewDispatchPointTwo;
    @ViewInject(R.id.tv_dispatch_price_one)
    private TextView textViewDispatchPriceOne;
    @ViewInject(R.id.tv_dispatch_price_two)
    private TextView textViewDispatchPriceTwo;
    @ViewInject(R.id.tv_tag_1)
    private TextView textViewTagOne;
    @ViewInject(R.id.tv_tag_2)
    private TextView textViewTagTwo;
    @ViewInject(R.id.iv_tag_2)
    private ImageView imageViewTagTwo;
    @ViewInject(R.id.iv_tag_3)
    private ImageView imageViewTagThree;
    @ViewInject(R.id.tv_tag_3)
    private TextView textViewTagThree;
    @ViewInject(R.id.tv_tag_4)
    private TextView textViewTagFour;
    @ViewInject(R.id.tv_discount_tag)
    private TextView textViewDiscountTag;
    @ViewInject(R.id.tv_discount)
    private TextView textViewDiscount;
//    @ViewInject(R.id.tv_deadline)
//    private TextView textViewDeadLine;
    @ViewInject(R.id.tv_tag_recharge1)
    private TextView textViewTagRecharge1;
    @ViewInject(R.id.tv_tag_recharge2)
    private TextView textViewTagRecharge2;
    @ViewInject(R.id.tv_tag_recharge3)
    private TextView textViewTagRecharge3;
    @ViewInject(R.id.ll_charge_promotion)
    private LinearLayout mLayoutPromotion2;

    private boolean shouldResult;
    private UserBean userBean;
    private RechargePlansBean plansBean;
    private int bidPoint;
    private GetRechargeResult recharge;

    @Override
    public String getName() {
        return null;
    }

    @Override
    public boolean isBar() {
        return false;
    }

    @Override
    public void initViews() {
        if (getIntent() != null) {
            shouldResult = getIntent().getBooleanExtra("result", false);
        }
    }

    @Override
    public void bindEvents() {
        mLayoutTitle.setOnClickBacktrack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void initData() {
        userBean = WPHSdk.getService(OAuthService.class).getUserInfo(null);
        setUserData(userBean);
        getUserInfo();
        WPHSdk.getService(UserInfoService.class)
                .getPlansList(new YRequestCallback<GetRechargeResult>() {
                    @Override
                    public void onSuccess(GetRechargeResult var1) {
                        setPackageData(var1);
                    }

                    @Override
                    public void onFailed(int var1, String message) {
                        showWarningText(message);
                    }

                    @Override
                    public void onException(Throwable var1) {
                        var1.printStackTrace();
                        showWarningText("出错了[" + var1.toString() + "]");
                    }
                });
    }

    private void getUserInfo() {
        userBean = WPHSdk.getService(OAuthService.class).getUserInfo(new YRequestCallback<UserBean>() {
            @Override
            public void onSuccess(UserBean var1) {
                userBean = var1;
                setUserData(userBean);
            }

            @Override
            public void onFailed(int var1, String message) {
                setUserData(userBean);
            }

            @Override
            public void onException(Throwable var1) {
                setUserData(userBean);
            }
        });
    }

    private void setPackageData(final GetRechargeResult var1) {
        if (var1 == null) return;
//        linearLayoutRechargeOne.setEnabled(false);
//        linearLayoutRechargeTwo.setEnabled(false);
//        linearLayoutRechargeThree.setEnabled(false);
//        linearLayoutSmsPackOne.setEnabled(false);
//        linearLayoutSmsPackTwo.setEnabled(false);
//        linearLayoutDispatchPackTwo.setEnabled(false);
//        linearLayoutDispatchPackOne.setEnabled(false);
        recharge = var1;
        if (var1.getDiscount_pack_set() == null || var1.getDiscount_pack_set().isOverTime()) {
            mLayoutPromotion2.setVisibility(View.GONE);
            textViewDiscountTag.setVisibility(View.GONE);
        } else {
            mLayoutPromotion2.setVisibility(View.VISIBLE);
            textViewDiscountTag.setVisibility(View.VISIBLE);
            //textViewDeadLine.setText("截止" + var1.getDiscount_pack_set().getDeadline().replaceAll("-", "."));
            textViewDiscount.setText(NumberUtil.double2StringAdaptive((double) var1.getDiscount_pack_set().getDiscount()) + "折充值");
            mLayoutPromotion.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showRechargeDialog(var1.getDiscount_pack_set().getDiscount());
                }
            });
        }

        if (var1.getPrice_pack_set() != null && var1.getPrice_pack_set().size() > 0) {
            for (int i = 0; i < (var1.getPrice_pack_set().size() > 3 ? 3 : var1.getPrice_pack_set().size()); i++) {
                if (i == 0) {
                    linearLayoutRechargeOne.setEnabled(true);
//                    textViewBidPointPackOne.setText(var1.getPrice_pack_set().get(0).getBid_point() + "汇币");
                    textViewBidPointPackOne.setText(var1.getPrice_pack_set().get(0).getName());
                    textViewBidPointPriceOne.setText(NumberUtil.double2StringAdaptive(var1.getPrice_pack_set().get(0).getPrice()) + "元");
                    if (var1.getPrice_pack_set().get(0).getGift_bid_point() > 0) {
                        textViewTagRecharge1.setVisibility(View.VISIBLE);
                        textViewTagRecharge1.setText("赠"+var1.getPrice_pack_set().get(0).getGift_bid_point());
                    }else {
                        textViewTagRecharge1.setVisibility(View.GONE);
                    }
                    linearLayoutRechargeOne.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            plansBean = var1.getPrice_pack_set().get(0);
                            payBidPointPack(plansBean.getId());
                        }
                    });
                } else if (i == 1) {
                    linearLayoutRechargeTwo.setEnabled(true);
//                    textViewBidPointPackTwo.setText(var1.getPrice_pack_set().get(1).getBid_point() + "汇币");
                    textViewBidPointPackTwo.setText(var1.getPrice_pack_set().get(1).getName());
                    textViewBidPointPriceTwo.setText(NumberUtil.double2StringAdaptive(var1.getPrice_pack_set().get(1).getPrice()) + "元");
                    if (var1.getPrice_pack_set().get(1).getGift_bid_point() > 0) {
                        textViewTagRecharge2.setVisibility(View.VISIBLE);
                        textViewTagRecharge2.setText("赠"+var1.getPrice_pack_set().get(1).getGift_bid_point());
                    }else {
                        textViewTagRecharge2.setVisibility(View.GONE);
                    }
                    linearLayoutRechargeTwo.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            plansBean = var1.getPrice_pack_set().get(1);
                            payBidPointPack(plansBean.getId());
                        }
                    });
                } else {
                    linearLayoutRechargeThree.setEnabled(true);
//                    textViewBidPointPackThree.setText(var1.getPrice_pack_set().get(2).getBid_point() + "汇币");
                    textViewBidPointPackThree.setText(var1.getPrice_pack_set().get(2).getName());
                    textViewBidPointPriceThree.setText(NumberUtil.double2StringAdaptive(var1.getPrice_pack_set().get(2).getPrice()) + "元");
                    if (var1.getPrice_pack_set().get(2).getGift_bid_point() > 0) {
                        textViewTagRecharge3.setVisibility(View.VISIBLE);
                        textViewTagRecharge3.setText("赠"+var1.getPrice_pack_set().get(2).getGift_bid_point());
                    }else {
                        textViewTagRecharge3.setVisibility(View.GONE);
                    }
                    linearLayoutRechargeThree.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            plansBean = var1.getPrice_pack_set().get(2);
                            payBidPointPack(plansBean.getId());
                        }
                    });
                }
            }
        }
        if (var1.getSms_pack_set() != null && var1.getSms_pack_set().size() > 0) {
            for (int i = 0; i < (var1.getSms_pack_set().size() > 3 ? 3 : var1.getSms_pack_set().size()); i++) {
                if (i == 0) {
                    linearLayoutSmsPackOne.setEnabled(true);
//                    if (var1.getSms_pack_set().get(0).isNot_limit()) {
//                        SpannableString spannableString = new SpannableString("无限量(全年)");
//                        spannableString.setSpan(new RelativeSizeSpan(0.6f), 3, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
//                        textViewSmsPointOne.setText(spannableString);
//                    } else {
//                        textViewSmsPointOne.setText(var1.getSms_pack_set().get(0).getSms_point() + "条");
//                    }
                    textViewSmsPointOne.setText(var1.getSms_pack_set().get(0).getName());
                    textViewSmsPriceOne.setText(var1.getSms_pack_set().get(0).getBid_point() + "汇币");
                    linearLayoutSmsPackOne.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            paySmsPointPack(var1.getSms_pack_set().get(0));
                        }
                    });
                    if (TextUtil.isNotEmpty(var1.getSms_pack_set().get(0).getTag())) {
                        textViewTagOne.setVisibility(View.VISIBLE);
                        textViewTagOne.setText(var1.getSms_pack_set().get(0).getTag());
                    } else {
                        textViewTagOne.setVisibility(View.GONE);
                    }
                } else if(i == 1){
                    linearLayoutSmsPackTwo.setEnabled(true);
//                    if (var1.getSms_pack_set().get(1).isNot_limit()) {
//                        SpannableString spannableString = new SpannableString("无限量(全年)");
//                        spannableString.setSpan(new RelativeSizeSpan(0.6f), 3, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
//                        textViewSmsPointTwo.setText(spannableString);
//                    } else {
//                        textViewSmsPointTwo.setText(var1.getSms_pack_set().get(1).getSms_point() + "条");
//                    }
                    textViewSmsPointTwo.setText(var1.getSms_pack_set().get(1).getName());
                    textViewSmsPriceTwo.setText(var1.getSms_pack_set().get(1).getBid_point() + "汇币");
                    linearLayoutSmsPackTwo.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            paySmsPointPack(var1.getSms_pack_set().get(1));
                        }
                    });
                    if (TextUtil.isNotEmpty(var1.getSms_pack_set().get(1).getTag())) {
                        textViewTagTwo.setVisibility(View.VISIBLE);
                        imageViewTagTwo.setVisibility(View.VISIBLE);
                        textViewTagTwo.setText(var1.getSms_pack_set().get(1).getTag());
                    } else {
                        textViewTagTwo.setVisibility(View.GONE);
                        imageViewTagTwo.setVisibility(View.GONE);
                    }
                }else{
                    linearLayoutSmsPackThree.setVisibility(View.VISIBLE);
                    linearLayoutSmsPackThree.setEnabled(true);
                    textViewSmsPointThree.setText(var1.getSms_pack_set().get(2).getName());
                    textViewSmsPriceThree.setText(var1.getSms_pack_set().get(2).getBid_point() + "汇币");
                    linearLayoutSmsPackThree.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            paySmsPointPack(var1.getSms_pack_set().get(2));
                        }
                    });
                    if (TextUtil.isNotEmpty(var1.getSms_pack_set().get(2).getTag())) {
                        textViewTagThree.setVisibility(View.VISIBLE);
                        imageViewTagThree.setVisibility(View.VISIBLE);
                        textViewTagThree.setText(var1.getSms_pack_set().get(2).getTag());
                    } else {
                        textViewTagThree.setVisibility(View.GONE);
                        imageViewTagThree.setVisibility(View.GONE);
                    }
                }
            }
        }

        if (var1.getDispatch_pack_set() != null && var1.getDispatch_pack_set().size() > 0) {
            for (int i = 0; i < (var1.getDispatch_pack_set().size() > 2 ? 2 : var1.getDispatch_pack_set().size()); i++) {
                if (i == 0) {
                    if (userBean != null && userBean.getUser().getLevel() > 10) {
                        linearLayoutDispatchPackOne.setEnabled(true);
                        linearLayoutDispatchPackOne.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                payDispatchPack(var1.getDispatch_pack_set().get(0));
                            }
                        });
                    } else {
                        linearLayoutDispatchPackOne.setOnClickListener(null);
                    }
//                    textViewDispatchPointOne.setText(var1.getDispatch_pack_set().get(0).getDispatch_point() + "单");
                    textViewDispatchPointOne.setText(var1.getDispatch_pack_set().get(0).getName());
                    textViewDispatchPriceOne.setText(var1.getDispatch_pack_set().get(0).getBid_point() + "汇币");
                    if (TextUtil.isNotEmpty(var1.getDispatch_pack_set().get(0).getTag())) {
                        textViewTagThree.setVisibility(View.VISIBLE);
                        textViewTagThree.setText(var1.getDispatch_pack_set().get(0).getTag());
                    } else {
                        textViewTagThree.setVisibility(View.GONE);
                    }
                } else {
                    if (userBean != null && userBean.getUser().getLevel() > 10) {
                        linearLayoutDispatchPackTwo.setEnabled(true);
                        linearLayoutDispatchPackTwo.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                payDispatchPack(var1.getDispatch_pack_set().get(1));
                            }
                        });
                    } else {
                        linearLayoutDispatchPackTwo.setOnClickListener(null);
                    }
//                    textViewDispatchPointTwo.setText(var1.getDispatch_pack_set().get(1).getDispatch_point() + "单");
                    textViewDispatchPointTwo.setText(var1.getDispatch_pack_set().get(1).getName());
                    textViewDispatchPriceTwo.setText(var1.getDispatch_pack_set().get(1).getBid_point() + "汇币");
                    if (TextUtil.isNotEmpty(var1.getDispatch_pack_set().get(1).getTag())) {
                        textViewTagFour.setVisibility(View.VISIBLE);
                        textViewTagFour.setText(var1.getDispatch_pack_set().get(1).getTag());
                    } else {
                        textViewTagFour.setVisibility(View.GONE);
                    }
                }
            }
        }
    }

    private void showRechargeDialog(final float discount) {
        View view = LayoutInflater.from(this).inflate(R.layout.view_dialog_recharge, null);
        final TextView textViewBidPoint = (TextView) view.findViewById(R.id.tv_bid_point);
        final SeekBar seekBar = (SeekBar) view.findViewById(R.id.seekbar);
        final int seekbarWidth = DensityUtil.getScreenWidth() - DensityUtil.dip2px(120);
        final TextView textViewTotal = (TextView) view.findViewById(R.id.tv_total);
        TextView textViewRecharge = (TextView) view.findViewById(R.id.tv_recharge);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                setProgress(progress, textViewBidPoint, textViewTotal, seekbarWidth, discount);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        textViewRecharge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MinePackageActivity2.this.bidPoint = (seekBar.getProgress() + 10) * 100;
                payBidPointPack2(bidPoint);
            }
        });
        seekBar.setProgress(10);
        setProgress(10, textViewBidPoint, textViewTotal, seekbarWidth, discount);
        YunBaseDialog dialog = BaseDialogManager.getInstance().getBuilder(this).setContentView(view).create();
        dialog.show();
    }

    private void setProgress(int progress, TextView textViewBidPoint, TextView textViewTotal, int seekbarWidth, float discount) {
        int bidPoint = (progress + 10) * 100;
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) textViewBidPoint.getLayoutParams();
        RelativeLayout.LayoutParams params2 = (RelativeLayout.LayoutParams) textViewTotal.getLayoutParams();

        if (bidPoint < 1000) {
            params.width = DensityUtil.dip2px(45);
            params2.width = DensityUtil.dip2px(50);
        } else if (bidPoint < 10000) {
            params.width = DensityUtil.dip2px(55);
            params2.width = DensityUtil.dip2px(60);
        } else {
            params.width = DensityUtil.dip2px(60);
            params2.width = DensityUtil.dip2px(70);
        }
        params.leftMargin = (int) (seekbarWidth / 99f * progress - (params.width / 2) + DensityUtil.dip2px(30));
        params2.leftMargin = (int) (seekbarWidth / 99f * progress - (params2.width / 2) + DensityUtil.dip2px(30));
        textViewBidPoint.setLayoutParams(params);
        textViewTotal.setLayoutParams(params2);
        textViewBidPoint.setText(bidPoint + "汇币");
        textViewTotal.setText("￥" + NumberUtil.double2StringAdaptive((double) (bidPoint * (discount / 10))));
    }

    private void payDispatchPack(final RechargePackBean rechargePackBean) {
        BaseDialogManager.getInstance().getBuilder(this)
                .setTitle("购买派单服务")
                .setIcon(ContextCompat.getDrawable(this, R.mipmap.toast_icon_half_off_hb))
                .setMessage("将扣除" + rechargePackBean.getBid_point() + "汇币购买" + rechargePackBean.getDispatch_point() + "单派单服务，无使用期限，随时可用")
                .setRightButtonText("确认购买")
                .setOnClickListener(new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        if (which == BaseDialogManager.RIGHT_BUTTON) {
                            WPHSdk.getService(UserInfoService.class).buyDispatchPoint(rechargePackBean.getId(), new YRequestCallback<Object>() {
                                @Override
                                public void onSuccess(Object var1) {
                                    getUserInfo();
                                }

                                @Override
                                public void onFailed(int var1, String message) {
                                    showWarningText(message);
                                }

                                @Override
                                public void onException(Throwable var1) {
                                    var1.printStackTrace();
                                    showWarningText("出错了[" + var1.toString() + "]");
                                }
                            });
                        }
                    }
                }).create().show();
    }

    private void paySmsPointPack(final RechargePackBean rechargePackBean) {
        String message = "";
        if (rechargePackBean.isNot_limit()) {
            long lastEndTime = System.currentTimeMillis();
            if (userBean != null && userBean.getPack() != null) {
                long time = TimeUtil.parseTime(userBean.getPack().getSms_end_date().replaceAll("-", "."), "yyyy.MM.dd");
                if (time > lastEndTime) {
                    lastEndTime = time;
                }
            }
            lastEndTime = lastEndTime + 31_622_400_000L;
            message = "将扣除" + rechargePackBean.getBid_point() + "汇币购买"+rechargePackBean.getDays()+"天无限量新项目提醒，有效期至" + TimeUtil.format(lastEndTime, "yyyy.MM.dd") + "日";
        } else {
            message = "将扣除" + rechargePackBean.getBid_point() + "汇币购买" + rechargePackBean.getSms_point() + "条新项目提醒，无使用期限，随时可用";
        }
        BaseDialogManager.getInstance().getBuilder(this)
                .setTitle("购买新项目提醒")
                .setIcon(ContextCompat.getDrawable(this, R.mipmap.toast_icon_half_off_hb))
                .setMessage(message)
                .setRightButtonText("确认购买")
                .setOnClickListener(new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        if (which == BaseDialogManager.RIGHT_BUTTON) {
                            WPHSdk.getService(UserInfoService.class).buySmsPoint(rechargePackBean.getId(), new YRequestCallback<Object>() {
                                @Override
                                public void onSuccess(Object var1) {
                                    getUserInfo();
                                }

                                @Override
                                public void onFailed(int var1, String message) {
                                    showWarningText(message);
                                }

                                @Override
                                public void onException(Throwable var1) {
                                    var1.printStackTrace();
                                    showWarningText("出错了[" + var1.toString() + "]");
                                }
                            });
                        }
                    }
                }).create().show();
    }

    private void payBidPointPack(int id) {
        PayManager.getInstance().payByWechat(id, new YRequestCallback() {
            @Override
            public void onSuccess(Object var1) {

            }

            @Override
            public void onFailed(int var1, String message) {
                showWarningText(message);
            }

            @Override
            public void onException(Throwable var1) {
                var1.printStackTrace();
            }
        });
    }

    private void payBidPointPack2(int bidPoint) {
        PayManager.getInstance().payDiscountByWechat(bidPoint, new YRequestCallback() {
            @Override
            public void onSuccess(Object var1) {

            }

            @Override
            public void onFailed(int var1, String message) {
                showWarningText(message);
            }

            @Override
            public void onException(Throwable var1) {
                var1.printStackTrace();
            }
        });
    }

    public void setUserData(UserBean bean) {
        if (bean != null && bean.getPack() != null) {
            textViewBidPoint.setText(Integer.toString(bean.getPack().getBid_point()));
            textViewDispatchPoint.setText(Integer.toString(bean.getPack().getDispatch_point()));
            if(TextUtil.isNotEmpty(bean.getPack().getSms_end_date()) && WTimeUtil.compareCurrentDate(bean.getPack().getSms_end_date())){
                textViewSmsPoint.setText("无限量");
            }else{
                textViewSmsPoint.setText(Integer.toString(bean.getPack().getSms_point()));
            }
            if (bean.getUser().getLevel() <= 10) {
                linearLayoutDispatchPackOne.setEnabled(false);
                linearLayoutDispatchPackTwo.setEnabled(false);
                textViewDispatchPoint.setEnabled(false);
                imageViewCopper.setImageResource(R.mipmap.my_icon_discount_copper_d);
                textViewLeftDispatchPoint.setTextColor(ContextCompat.getColor(MinePackageActivity2.this, R.color.colorTextPrimaryHint));
                textViewDispatchExplain.setTextColor(ContextCompat.getColor(MinePackageActivity2.this, R.color.colorTextPrimaryHint));
            } else {
                linearLayoutDispatchPackOne.setEnabled(true);
                linearLayoutDispatchPackTwo.setEnabled(true);
                textViewDispatchPoint.setEnabled(true);
                imageViewCopper.setImageResource(R.mipmap.my_icon_discount_copper_a);
                textViewLeftDispatchPoint.setTextColor(ContextCompat.getColor(MinePackageActivity2.this, R.color.colorTextPrimaryOne));
                textViewDispatchExplain.setTextColor(ContextCompat.getColor(MinePackageActivity2.this, R.color.colorTextPrimaryOne));
            }
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        PayManager.getInstance().addPayResultListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        PayManager.getInstance().removePayResultListener(this);
    }

    @Override
    public void paySuccess(long orderId, int bidPoint) {
        if (plansBean != null) {
            BaseDialogManager.getInstance().getBuilder(this)
                    .setIcon(ContextCompat.getDrawable(this, R.drawable.lib_toast_icon_ok))
                    .setTitle("支付成功")
                    .setMessage("已新增" + plansBean.getBid_point() + "个汇币")
                    .setRightButtonText("我知道了")
                    .setOnClickListener(new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    })
                    .create()
                    .show();
        } else if (bidPoint > 0) {
            BaseDialogManager.getInstance().getBuilder(this)
                    .setIcon(ContextCompat.getDrawable(this, R.drawable.lib_toast_icon_ok))
                    .setTitle("支付成功")
                    .setMessage("已新增" + bidPoint + "个汇币")
                    .setRightButtonText("我知道了")
                    .setOnClickListener(new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    })
                    .create()
                    .show();
        } else {
            ToastManager.getInstance().showToast("支付成功");
        }
        plansBean = null;
        this.bidPoint = 0;
        getUserInfo();
    }

    @Override
    public void payFail(int code, String message) {
        BaseDialogManager.getInstance().getBuilder(this)
                .setIcon(ContextCompat.getDrawable(this, R.drawable.lib_toast_icon_mistake))
                .setTitle("支付失败")
                .setMessage("您支付失败或取消支付")
                .setRightButtonText("重新支付")
                .setOnClickListener(new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (which == BaseDialogManager.RIGHT_BUTTON) {
                            if (plansBean != null) {
                                payBidPointPack(plansBean.getId());
                            } else if (bidPoint > 0) {
                                payBidPointPack2(bidPoint);
                            }
                        }
                        dialog.dismiss();
                    }
                })
                .create()
                .show();
    }
}
